﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class ChiTietNhapHangDA
    {
        /// <summary>
        /// 
        /// </summary>
        public ChiTietNhapHangDA()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(ChiTietNhapHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHDNH", entity.MaHDNH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("GiaNhap", entity.GiaNhap);
                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietNhapHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int Insert(ChiTietNhapHangEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaHDNH", entity.MaHDNH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("SoLuong", entity.SoLuong);
                pb.AddParameter("GiaNhap", entity.GiaNhap);
                return (int)DBFactory.Database.ExecuteNonQuery("ChiTietNhapHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate_MaHDNH(ChiTietNhapHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaChiTietNH", entity.MaChiTietNH);
                pb.AddParameter("MaHDNH", entity.MaHDNH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("Gia", entity.GiaNhap);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("ChiTietNhapHang_UpDate_MaHDNH", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate_MaChiTietNH(ChiTietNhapHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("MaChiTietNH", entity.MaChiTietNH);
                pb.AddParameter("MaHDNH", entity.MaHDNH);
                pb.AddParameter("MaModelMH", entity.MaModelMH);
                pb.AddParameter("Gia", entity.GiaNhap);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("ChiTietNhapHang_UpDate_MaChiTietNH", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetAll()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM ChiTietNhapHang");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public DataTable Find_MaChiTietNH(int ten)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaChiTietNH", ten);

                dt = DBFactory.Database.FillDataTable("ChiTietNhapHang_Find_MaChiTietNH", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable Find_MaHDNH(int id)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaHDNH", id);

                dt = DBFactory.Database.FillDataTable("ChiTietNhapHang_Find_MaHD", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaNhapHang"></param>
        /// <returns></returns>
        public DataTable GetInfo_ByMaNhapHang(int MaNhapHang)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaNhapHang", MaNhapHang);

                return DBFactory.Database.FillDataTable("ChiTietNhapHang_GetInfo_ByMaNhapHang", pd.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<ChiTietNhapHangEntity> ConvertToList(DataTable dt)
        {
            List<ChiTietNhapHangEntity> list = new List<ChiTietNhapHangEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int id = (int)dt.Rows[i][0];
                int mahd = (int)dt.Rows[i][1];
                int mamodel = (int)dt.Rows[i][2];
                int soluong = (int)dt.Rows[i][3];
                decimal gianhap = (decimal)dt.Rows[i][4];

                ChiTietNhapHangEntity entiry = new ChiTietNhapHangEntity(id, mahd, mamodel,soluong, gianhap);
                list.Add(entiry);
            }
            return list;
        }
    }
}

